<?php

namespace app\service;

use app\tools\Workerman;
use Exception;

/**
 * 聊天
 *
 * user ：Du
 * Date : 2022/3/26
 * Time : 14:44
 * Class ChatServices
 * @package projects\services
 */
class ChatServices
{
    /**
     * 用户发言
     *
     * User : Du
     * Date : 2022/3/26
     * Time : 14:55
     * @param array $request
     * @return bool
     * @throws Exception
     */
    public static function sendMessage(array $request): bool
    {
        if (session('manage_info')['id'] === 1) {
            $avatar = '/admin/chat/women_avatar2.jpg';
        } else {
            $avatar = '/admin/chat/man_avatar2.jpg';
        }
        $data = [
            'user_id' => session('manage_info')['id'],
            'type' => 'say',
            'time' => date('H:i:s'),
            'username' => session('manage_info')['nickname'],
            'avatar' => $avatar,
            'content' => $request['data'],
        ];
        Workerman::sendMessage($data, 'all');
        return true;
    }

    /**
     * 用户输入状态
     *
     * User : Du
     * Date : 2022/3/26
     * Time : 16:58
     * @param string $type
     * @return bool
     * @throws Exception
     */
    public static function inputStatus(string $type): bool
    {
        $data = [
            'user_id' => session('manage_info')['id'],
            'status' => $type,
            'type' => 'inputStatus'
        ];
        Workerman::sendMessage($data, 'all');
        return true;
    }
}